package rush.edu.wechat.common.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

/**
 * DESC:BaseRedis 配置
 * USER:hmily
 * DATE:2018/1/31
 * TIME:19:33
 */
@Configuration
public class DefaultRedisConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory(RedisConfig config,JedisPoolConfig jedisPoolConfig){
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setHostName(config.getHost());
        connectionFactory.setPort(config.getPort());
        connectionFactory.setPassword(config.getPassword());
        connectionFactory.setDatabase(config.getDatabase());
        connectionFactory.setPoolConfig(jedisPoolConfig);
        return connectionFactory;
    }

    @Bean
    public JedisPoolConfig jedisPoolConfig(RedisConfig config){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        //poolConfig.setMaxIdle(config.getMaxIdle());
        //poolConfig.setMaxTotal(config.get);
        return poolConfig;
    }


    @Bean
    public RedisTemplate redisTemplate(JedisConnectionFactory connectionFactory){
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}
